﻿using System;
using DALHelper.DAL;
using System.Collections.Generic;

namespace DALHelper.Business
{
    public class TransDetail
    {
        private DALManager m_DataManager;

        #region properties
        public string TransDetailID
        {
            get;
            set;
        }

        public string TransactionID
        {
            get;
            set;
        }

        public string GuestName
        {
            get;
            set;
        }

        public string GuestIDNumber
        {
            get;
            set;
        }

        public DateTime StartTime
        {
            get;
            set;
        }

        public DateTime EndTime
        {
            get;
            set;
        } 
       

        /// <summary>
        /// Don gia
        /// </summary>
        public decimal RoomFee
        {
            get;
            set;
        }

        public decimal CompensationFee
        {
            get;
            set;
        }

        /// <summary>
        /// Thanh tien
        /// </summary>
        public decimal RoomCost
        {
            get;
            set;
        }

        public string RoomID
        {
            get;
            set;
        }

        public string TokenID
        {
            get;
            set;
        }
        #endregion

        public TransDetail(DALManager dtManager)
        {
            m_DataManager = dtManager;

        }

        public TransDetail()
        {
            GuestName = "";
            GuestIDNumber = "";
            RoomFee = 0;
            CompensationFee = 0;
            RoomCost = 0;
        }

        public void AddTransactionDetail(TransDetail[] t, int n, int wait)
        {
            try
            {

                m_DataManager.TransDetailDAL.addTransDetail(t[0].TransactionID, parseTransDetailInfo(t, n), wait);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void DeleteTransactionDetail(TransDetail[] t, int n, int wait)
        {
            try
            {
                m_DataManager.TransDetailDAL.delTransDetail(t[0].TransactionID, parseTransactionDetailID(t, n), wait);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void UpdateTransactionDetails(IEnumerable<TransDetail> arrDetail)
        {
            m_DataManager.TransDetailDAL.UpdateTransDetail(arrDetail);
        }

        //public void UpdateTransactionDetail(TransDetail t, int wait)
        //{
        //    try
        //    {
        //        m_DataManager.TransDetailDAL.updateTransDetail(t.TransactionID, t.TransDetailID, t.GuestName, t.GuestIDNumber,
        //            t.StartTime, t.EndTime, t.RoomID, wait);
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }
        //}

        public string parseTransDetailInfo(TransDetail[] t, int n)
        {
            string strTransDetail = "";
            int i;
            for (i = 0; i < n; i++)
            {
                strTransDetail += t[i].GuestName;
                strTransDetail += ',';
                strTransDetail += t[i].GuestIDNumber;
                strTransDetail += ',';
                strTransDetail += t[i].StartTime;
                strTransDetail += ',';
                strTransDetail += t[i].EndTime;
                strTransDetail += ',';
                strTransDetail += t[i].RoomID;
                strTransDetail += ',';
                strTransDetail += t[i].TokenID;

                if (i < n - 1)
                {
                    strTransDetail += ',';
                }
            }

            return strTransDetail;
        }

        public string parseTransactionDetailID(TransDetail[] t, int n)
        {
            string strTransDetailID = "";

            int i = 0;

            for (i = 0; i < n; i++)
            {
                strTransDetailID += t[i].TransDetailID;
                if (i < n - 1)
                {
                    strTransDetailID += ',';
                }
            }

            return strTransDetailID;
        }

        //public void BookRooms(string strGroupCode, RoomCategory[] strRoomTypes, int[] iQuestNumbers, int Wait)
        //{
        //    try
        //    {
        //        int i;
        //        string strDetail = "";
        //        int n = strRoomTypes.Length;

        //        for (i = 0; i < n; i++)
        //        {
        //            strDetail += strRoomTypes[i].RoomCatID;
        //            strDetail += ',';
        //            strDetail += iQuestNumbers[i];

        //            if (i < n - 1)
        //            {
        //                strDetail += ',';
        //            }
        //        }

        //        m_DataManager.TransDetailDAL.bookRooms(strGroupCode, strDetail, Wait);
        //    }
        //    catch(Exception e)
        //    {
        //        throw e;
        //    }
        //}

        public void BookRooms(long lTransID, RoomCategory[] arrRoomTypes)
        {
            try
            {
                string id = "000" + lTransID;
                id = "GD" + id.Substring(id.Length - 3);

                string strDetail = string.Empty;
                int n = arrRoomTypes.Length;

                for (int i = 0; i < n; i++)
                {
                    strDetail += arrRoomTypes[i].RoomCatID;
                    strDetail += ',';
                    strDetail += arrRoomTypes[i].BookedGuestCount;

                    if (i < n - 1)
                    {
                        strDetail += ',';
                    }
                }

                m_DataManager.TransDetailDAL.bookRooms(id, strDetail, 0);
            }
            catch(Exception e)
            {
                throw e;
            }
        }
    }
}
